<?php

namespace App\Services\impl;

use App\Services\spider\ITypeParser;
use App\Services\spider\IUrParser;
use App\Services\spider\ParseResult;
use App\Services\UtilService;
use Illuminate\Support\Str;
use SimpleHtmlDom\simple_html_dom;

class QgyydqTypeParser extends ITypeParser
{


    public function parseHtml(string $url, string $htmlContent): ParseResult
    {
        try {
            $html = new simple_html_dom();
            $html->load($htmlContent);
            $tBody = $html->find(".main .content ul table tbody tr table tbody");
            $nodes = $tBody[0]->children;
            $arr = [];
            foreach ($nodes as $node) {
                $key = $node->children[0]->innertext;
                $value = $node->children[1]->innertext;
                if (Str::contains($key, "医院名称")) {
                    $arr["name"] = $value;
                } else if (Str::contains($key, "联系方式")) {
                    $arr["contact"] = $value;
                } else if (Str::contains($key, "医院地址")) {
                    $arr["address"] = $value;
                } else if (Str::contains($key, "法人")) {
                    $arr["fa_ren"] = $value;
                } else if (Str::contains($key, "负责人")) {
                    $arr["fu_ze_ren"] = $value;
                }
            }
            if ($arr['name'] ?? false) {
                return ParseResult::of(true, $arr);
            }
        } catch (\Exception $e) {
            UtilService::getInstance()->print("parse url = $url , html error");
        }
        return ParseResult::of();

    }


}